package com.cloud.school.bus.teacherhelper.modules.home.adapter;

import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;

import com.android.support.jhf.utils.DateUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Picture;
import com.cloud.school.bus.teacherhelper.entitys.UploadFile;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.ExifInterface;
import android.media.ThumbnailUtils;
import android.os.Debug;
import android.provider.MediaStore.Video.Thumbnails;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VideoSelectedAdapter extends BaseAdapter {

	private Context mContext;
	private TeacherHelperApplication mApplication;
	private List<Picture> mPictures;
	/** true标示删除上传文件 */
	private boolean mDeleteUploadFile = false;
	/** 列表第一条的view用来更新进度 */
	private View mFirstView;

	public VideoSelectedAdapter(Context context, List<Picture> pictures) {
		mContext = context;
		mApplication = (TeacherHelperApplication) mContext
				.getApplicationContext();
		mPictures = pictures;
	}

	@Override
	public int getCount() {
		return mPictures.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mPictures.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		if (null == arg1) {
			arg1 = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_video_selected, null);
		}

		Picture picture = mPictures.get(arg0);

		ImageView leftImageView = (ImageView) arg1
				.findViewById(R.id.leftImageView);
		leftImageView.setImageResource(R.drawable.ic_default_video);
		if (!TextUtils.isEmpty(picture.getPicturePath())) {
			if (picture.getPicturePath().endsWith("MP4")
					|| picture.getPicturePath().endsWith("mp4")) {
				Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(
						picture.getPicturePath().replace("file:///", "/"),
						Thumbnails.MINI_KIND);
				if (null != bitmap) {
					leftImageView.setImageBitmap(bitmap);
				} 
			}
		}

		TextView videoDateTextView = (TextView) arg1
				.findViewById(R.id.videoDateTextView);
		File file = new File(picture.getPicturePath().replace("file:///", "/"));
		try {
			videoDateTextView.setText(DateUtils.dateFormat(file.lastModified(),
					"yyyy-MM-dd HH:mm:ss"));
		} catch (Exception e) {
		}
		TextView deleteTextView = (TextView) arg1
				.findViewById(R.id.deleteTextView);
		if (mDeleteUploadFile) {
			deleteTextView.setVisibility(View.VISIBLE);
		} else {
			deleteTextView.setVisibility(View.GONE);
		}

		return arg1;
	}

	public View getFirstView() {
		return mFirstView;
	}

	public void setDeleteUploadFile(boolean deleteUploadFile) {
		mDeleteUploadFile = deleteUploadFile;
	}

	public boolean getDeleteUploadFile() {
		return mDeleteUploadFile;
	}

}
